/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.spring.plugin;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.cxf.common.logging.Log4jLogger;
import org.apache.cxf.common.logging.LogUtils;

import com.genia.toolbox.basics.exception.technical.TechnicalException;
import com.genia.toolbox.spring.provider.plugin.impl.AbstractSimpleSpringProviderPlugin;
import com.genia.toolbox.web.servlet.CustomContextLoaderListener;

/**
 * The Plugin that allow to automatically declares Spring beans.
 */
public class WebSpringProviderPlugin
    extends AbstractSimpleSpringProviderPlugin
{

  /**
   * the main config file.
   */
  private static final String MAIN_CONFIG_FILE_RESOURCE = "/com/genia/toolbox/web/spring/web-spring-config.xml";

  /**
   * the web service config file.
   */
  private static final String CXF_WS_CONFIG_FILE_RESOURCE = "/com/genia/toolbox/web/spring/cxf-ws-spring-config.xml";

  /**
   * the portlet description config file.
   */
  private static final String PORTLET_DESCRIPTION_CONFIG_FILE_RESOURCE = "/com/genia/toolbox/web/spring/layoutDescription-spring-config.xml";



  /**
   * Constructor.
   */
  public WebSpringProviderPlugin()
  {
    super(MAIN_CONFIG_FILE_RESOURCE, PORTLET_DESCRIPTION_CONFIG_FILE_RESOURCE);
    System.setProperty(LogUtils.KEY, Log4jLogger.class.getName());
  }
  


  /**
   * Return a {@link Collection} of {@link InputStream} that contains Spring
   * config files.
   * 
   * @return a {@link Collection} of {@link InputStream} that contains Spring
   *         config files
   * @throws TechnicalException
   *           when a configuration file does not exists
   * @see com.genia.toolbox.spring.provider.plugin.SpringProviderPlugin#getConfigurations()
   */
  @Override
  public Collection<InputStream> getConfigurations()
      throws TechnicalException
  {
    List<InputStream> result = new ArrayList<InputStream>(super.getConfigurations());
    if(CustomContextLoaderListener.isWebApplicationContextInitializing()) {
      result.add(getClass().getResourceAsStream(CXF_WS_CONFIG_FILE_RESOURCE));
    }
    return result;
  }

}
